package com.mq.admin;

import org.apache.kafka.clients.admin.Admin;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.NewTopic;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname AdminTopic
 * @Description
 * @Version 1.0.0
 * @Date 2024/12/11 10:04
 * @Created by ncs
 */
public class AdminTopic {

    public static void main(String[] args) {
        HashMap<String, Object> map = new HashMap<>();
        map.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.125.100:9094");
        try (Admin admin = Admin.create(map)){
            int numPartitions1 = 1;
            short replicationFactor1 = 1;
            NewTopic newTopic1 = new NewTopic("czp_admin_topic1", numPartitions1, replicationFactor1);
            int numPartitions2 = 2;
            short replicationFactor2 = 2;
            NewTopic newTopic2 = new NewTopic("czp_admin_topic2", numPartitions2, replicationFactor2);
            admin.createTopics(List.of(newTopic1, newTopic2));
            //自定义分区 key是副本编号 value是brokerId(第一位是leader)
            Map<Integer, List<Integer>> replicationMap = new HashMap<>();
            replicationMap.put(0, List.of(3,1));
            replicationMap.put(1, List.of(1,2));
            replicationMap.put(2, List.of(2,3));
            NewTopic newTopic3 = new NewTopic("czp_admin_topic3", replicationMap);
            admin.createTopics(List.of(newTopic3));
        }
    }
}
